package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.Contract;
import cn.wolfcode.car.business.domain.Customer;
import cn.wolfcode.car.business.mapper.ContractMapper;
import cn.wolfcode.car.business.query.ContractQuery;
import cn.wolfcode.car.business.service.IContractService;
import cn.wolfcode.car.business.service.ICustomerService;
import cn.wolfcode.car.business.service.impl.BpmnInfoServiceImpl;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.util.file.FileUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/contract")
public class ContractController {
    //模板前缀
    private static final String prefix = "business/contract/";

    @Autowired
    private IContractService contractService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private BpmnInfoServiceImpl bpmnInfoService;

    //页面------------------------------------------------------------
    //列表
    /*@RequiresPermissions("business:contract:view")*/
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    /*@RequiresPermissions("business:contract:add")*/
    @RequestMapping("/addPage")
    public String addPage(Model model){
        List<Customer> customers = customerService.list();
        model.addAttribute("customers",customers);

        // 新增合同时候，顺便发起审核需要用到bpmnInfoId、审核节点负责人暂写死为 杨龙
        List<BpmnInfo> bpmnInfos = bpmnInfoService.queryByType("contract");
        BpmnInfo bpmnInfo = bpmnInfos.get(0);
        model.addAttribute("bpmnInfoId",bpmnInfo.getId());
        model.addAttribute("director",3);
        return prefix + "add";
    }


    /*@RequiresPermissions("business:contract:edit")*/
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("contract", contractService.get(id));
        model.addAttribute("customers",customerService.list());
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    /*@RequiresPermissions("business:contract:list")*/
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Contract> query(ContractQuery qo){
        return contractService.query(qo);
    }

    //新增 发起合同审核流程
    /*@RequiresPermissions("business:contract:add")*/
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Contract contract,Long bpmnInfoId,Long director){
        contractService.save(contract,bpmnInfoId,director);
        return AjaxResult.success();
    }

    //编辑
    /*@RequiresPermissions("business:contract:edit")*/
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Contract contract){
        contractService.update(contract);
        return AjaxResult.success();
    }

    //删除
    /*@RequiresPermissions("business:contract:remove")*/
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        contractService.deleteBatch(ids);
        return AjaxResult.success();
    }

    //新增时候上传合同电子附件
    @RequestMapping("/uploadFile")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file) throws IOException {
        String path = contractService.uploadFile(file);
        return AjaxResult.success("操作成功",path);
    }

    //新增时候上传合同电子附件2
    @PostMapping("/uploadFileMinio")
    @ResponseBody
    public AjaxResult uploadFileMinio(MultipartFile file) throws Exception {
        String url = contractService.uploadFileMinio(file);
        return AjaxResult.success("操作成功",url);
    }

    //新增时候上传合同电子附件3
    @RequestMapping("/uploadFile2")
    @ResponseBody
    public AjaxResult uploadFile2(MultipartFile file) throws IOException {
        String path = contractService.uploadFile2(file);
        return AjaxResult.success("操作成功",path);
    }

    //下载合同文件
    @RequestMapping("/downloadContract")
    @ResponseBody
    public void downloadContract(Long id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Contract contract = contractMapper.selectByPrimaryKey(id);
        // 获取合同文件名
        String fileName = contract.getAppendix();

        String realPath = contractService.downloadContract(id,request);
        Files.copy(Paths.get(realPath,fileName),response.getOutputStream());
    }

    //下载合同文件minio
    @RequestMapping("/downloadContractMinio")
    @ResponseBody
    public void downloadContractMinio(Long id,HttpServletResponse response) throws IOException {
        Contract contract = contractMapper.selectByPrimaryKey(id);
        // 获取合同文件名
        String fileName = contract.getAppendix();

        response.sendRedirect(fileName);
    }

    //下载合同文件2
    @RequestMapping("/downloadContract2")
    @ResponseBody
    public void downloadContract2(Long id,HttpServletResponse response) throws IOException {
        contractService.downloadContract2(id,response);
    }

    //合同作废
    @RequestMapping("/useless")
    @ResponseBody
    public AjaxResult useless(Long id) {
        contractService.useless(id);
        return AjaxResult.success();
    }

    //审核通过
    @RequestMapping("/approve")
    @ResponseBody
    public AjaxResult approve(Long id) {
        contractService.approve(id);
        return AjaxResult.success();
    }

    //审核不通过
    @RequestMapping("/unapprove")
    @ResponseBody
    public AjaxResult unapprove(Long id) {
        contractService.unapprove(id);
        return AjaxResult.success();
    }

    //盖章
    @RequestMapping("/affixSeal")
    @ResponseBody
    public AjaxResult affixSeal(Long id) {
        contractService.affixSeal(id);
        return AjaxResult.success();
    }
}
